﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace Convertor.Plugin.Encrypt
{
    class ShaMethod
    {
        public static string Encrypt(Encoding encoder, string source, EncryptTypes hashType)
        {
            byte[] bt = encoder.GetBytes(source);
            HashAlgorithm hashModel;
            switch (hashType)
            {
                default:
                case EncryptTypes.SHA1:
                    hashModel = SHA1.Create();
                    break;
                case EncryptTypes.SHA256:
                    hashModel = SHA256.Create();
                    break;
                case EncryptTypes.SHA384:
                    hashModel = SHA384.Create();
                    break;
                case EncryptTypes.SHA512:
                    hashModel = HMACSHA512.Create();
                    break;
                case EncryptTypes.Md5:
                    hashModel = MD5.Create();
                    break;
            }
            bt = hashModel.ComputeHash(bt);
            StringBuilder sb = new StringBuilder();
            foreach (byte iByte in bt)
            {
                sb.AppendFormat("{0:x2}", iByte);
            }
            hashModel.Clear();
            return sb.ToString();
        }


    }
}
